package org.blame.lotterysystem.controller;

import org.blame.lotterysystem.common.errorcode.ControllerErrorCodeConstants;
import org.blame.lotterysystem.common.exception.ControllerException;
import org.blame.lotterysystem.common.pojo.CommonResult;
import org.blame.lotterysystem.common.utils.JacksonUtil;
import org.blame.lotterysystem.controller.param.CreateActivityParam;
import org.blame.lotterysystem.controller.param.PageParam;
import org.blame.lotterysystem.controller.result.ActivityResult;
import org.blame.lotterysystem.controller.result.FindActivityListResult;
import org.blame.lotterysystem.controller.result.GetActivityDetailResult;
import org.blame.lotterysystem.service.ActivityService;
import org.blame.lotterysystem.service.dto.ActivityDTO;
import org.blame.lotterysystem.service.dto.ActivityDetailDTO;
import org.blame.lotterysystem.service.dto.ActivityListDTO;
import org.blame.lotterysystem.service.dto.PageListDTO;
import org.blame.lotterysystem.service.enums.ActivityPrizeTiersEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.w3c.dom.stylesheets.LinkStyle;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

@RestController
public class ActivityController {

    private static final Logger logger= LoggerFactory.getLogger(ActivityController.class);

    @Autowired
    private ActivityService activityService;

    @RequestMapping("/activity/create")
    public CommonResult<ActivityResult> createActivity(@Validated @RequestBody CreateActivityParam activityParam){
        logger.info("createActivity  CreateActivityParam:{}",activityParam);

        return CommonResult.success(convertActivityResult(activityService.createActivity(activityParam)));

    }

    @RequestMapping("/activity/find-list")
    public CommonResult<FindActivityListResult> findActivityList(PageParam param){
        logger.info(" findActivityList  param:{}", JacksonUtil.writeValueAsString(param));
        PageListDTO<ActivityListDTO> activityListDTO=activityService.findActivityList(param);
        return CommonResult.success(convertActivityListDTOToFindActivityListResult(activityListDTO));
    }

    @RequestMapping("/activity-detail/find")
    public CommonResult<GetActivityDetailResult> getActivityDetail(Long activityId) {
        logger.info("getActivityDetail activityId:{}",activityId);
        ActivityDetailDTO detailDTO=activityService.getActivityDetailById(activityId);
        return CommonResult.success(convertToGetActivityDetailResult(detailDTO));

    }

    private GetActivityDetailResult convertToGetActivityDetailResult(ActivityDetailDTO detailDTOList) {
        if(detailDTOList==null){
            throw new ControllerException(ControllerErrorCodeConstants.GET_ACTIVITY_DETAIL_ERROR);
        }
        GetActivityDetailResult getActivityDetailResult=new GetActivityDetailResult();
        getActivityDetailResult.setActivityId(detailDTOList.getActivityId());
        getActivityDetailResult.setActivityName(detailDTOList.getActivityName());
        getActivityDetailResult.setDescription(detailDTOList.getDescription());
        getActivityDetailResult.setValid(detailDTOList.valid());
        getActivityDetailResult.setUsers(
                detailDTOList.getUserDTOList()
                        .stream()
                        .map(userDTO -> {
                            GetActivityDetailResult.User user=new GetActivityDetailResult.User();
                            user.setValid(userDTO.valid());
                            user.setUserId(userDTO.getUserId());
                            user.setUserName(userDTO.getUserName());
                            return user;
                        }).collect(Collectors.toList())
        );
        getActivityDetailResult.setPrizes(
                detailDTOList.getPrizeDTOList()
                        .stream()
                        .sorted(Comparator.comparingInt(prizeDTO->prizeDTO.getTiers().getCode()))
                        .map(prizeDTO -> {
                            GetActivityDetailResult.Prize prize=new GetActivityDetailResult.Prize();
                            prize.setPrizeId(prizeDTO.getPrizeId());
                            prize.setPrizeAmount(prizeDTO.getPrizeAmount());
                            prize.setValid(prizeDTO.valid());
                            prize.setName(prizeDTO.getName());
                            prize.setPrice(prizeDTO.getPrice());
                            prize.setPrizeTierName(prizeDTO.getTiers().name());
                            prize.setDescription(prizeDTO.getDescription());
                            prize.setImageUrl(prizeDTO.getImageUrl());
                            return prize;
                        }).collect(Collectors.toList())
        );
        return getActivityDetailResult;
    }


    private FindActivityListResult convertActivityListDTOToFindActivityListResult(PageListDTO<ActivityListDTO> activityListDTO) {
        FindActivityListResult findActivityListResult=new FindActivityListResult();
        findActivityListResult.setTotal(activityListDTO.getTotal());
        findActivityListResult.setRecords(activityListDTO
                .getRecords()
                .stream()
                .map(activityDTO -> {
                    FindActivityListResult.ActivityInfo activityInfo=new FindActivityListResult.ActivityInfo();
                    activityInfo.setActivityId(activityDTO.getActivityId());
                    activityInfo.setActivityName(activityDTO.getActivityName());
                    activityInfo.setValid(activityDTO.valid());
                    activityInfo.setDescription(activityDTO.getDescription());
                    return activityInfo;
                }).collect(Collectors.toList()));
        return findActivityListResult;
    }



    private ActivityResult convertActivityResult(ActivityDTO activity) {
        ActivityResult activityResult=new ActivityResult();
        activityResult.setActivityId(activityResult.getActivityId());
        return activityResult;
    }
}
